/**
 * Created by gongxun on 17/5/2.
 */

var KwxGiftExchangeItem = GameBaseItem.extend({
    Image_select : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxGiftExchangeItem.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Image_select = mo.seekWidgetByName(this.ccsNode, "Image_select");

        this.setContentSize(213, 159);
        this.setAnchorPoint(cc.p(0.5, 0.5));
        this.ccsNode.setPosition(cc.p(213 / 2, 159 / 2));
        this.setTouchEnabled(true);

        this.setSelect(false);
    },

    setItemInfo : function(itemInfo){
        //价值
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_price"), itemInfo.money + "元");

        //消耗
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_cost"), "" + itemInfo.cost_item_num);

        //图片
        mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(this.ccsNode, "Image_url"), itemInfo.image);
    },

    setSelect : function(isSelect){
        this.Image_select.setVisible(isSelect);
    }
});

var KwxGiftExchangeLayer = KwxSecondBase.extend({
    PageView_list: null,
    mMsgIds: null,
    mItems : null,
    mData : null,

    mSelectIndex : null,

    //自己的礼券
    Text_myLottery : null,
    //选中的礼券
    Text_costLottery : null,
    //确定按钮
    Button_send : null,

    //填写详细信息
    Panel_second : null,
    //输入
    mInputBox : null,

    //分页
    Image_fenyes : null,
    ctor: function () {
        this._super();
        this.loadSecondCCS("res/ccs/KwxGiftExchangeLayer.json");
        this.setTitleText("res/ccs/hall_second/text/text_liquanhuanhaoli.png");

        this.PageView_list = mo.seekWidgetByName(this.ccsNode, "PageView_list");
        this.Text_myLottery = mo.seekWidgetByName(this.ccsNode, "Text_myLottery");
        this.Text_costLottery = mo.seekWidgetByName(this.ccsNode, "Text_costLottery");
        this.Button_send = mo.seekWidgetByName(this.ccsNode, "Button_send");
        this.Panel_second = mo.seekWidgetByName(this.ccsNode, "Panel_second");

        //输入
        var inputP = mo.seekWidgetByName(this.ccsNode, "Image_inputBg").getPosition();
        var inputS = mo.seekWidgetByName(this.ccsNode, "Image_inputBg").getContentSize();

        inputP = cc.p(inputP.x + 5, inputP.y);
        inputS = cc.size(inputS.width - 10, inputS.height - 30);
        this.mInputBox = new cc.EditBox(inputS, new cc.Scale9Sprite("res/ccs/common/Transparent.png"));
        this.mInputBox.setString("");
        this.mInputBox.setPosition(inputP);
        this.mInputBox.setReturnType(cc.KEYBOARD_RETURNTYPE_SEND);
        this.mInputBox.setInputMode(cc.EDITBOX_INPUT_MODE_PHONENUMBER);
        this.mInputBox.setFontColor(cc.color(255, 255, 255));
        this.mInputBox.setFontSize(30);
        this.mInputBox.setPlaceHolder("请输入电话号码");
        this.mInputBox.setPlaceholderFontColor(cc.color(255, 255, 255));
        this.mInputBox.setMaxLength(11);
        this.Panel_second.addChild(this.mInputBox);

        //分页
        this.Image_fenyes = [];
        var Panel_fenye = mo.seekWidgetByName(this.ccsNode, "Panel_fenye");
        for (var i = 0; i < 6; i++){
            var Image_fenye = mo.seekWidgetByName(Panel_fenye, "Image_fenye" + i);
            this.Image_fenyes.push(Image_fenye);
            Image_fenye.setVisible(false);
        }

        this.PageView_list.addEventListener(function(sender, type){
            this.updateIndex();
        }, this);

        //确定
        mo.seekWidgetByName(this.ccsNode, "Button_send").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                this.Panel_second.setVisible(true);
            }
        }, this);

        //关闭二级页面
        mo.seekWidgetByName(this.ccsNode, "Button_secondClose").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                this.Panel_second.setVisible(false);
            }
        }, this);

        //发送给服务器
        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                var mobileNum = this.mInputBox.getString();
                if (!mo.gameHelper.getIsPhoneNum(mobileNum)){
                    mo.showRoomMsgShow("请输入有效的手机号码！");
                    return;
                }

                if (this.mSelectIndex == null){
                    return;
                }

                var info = this.mData[this.mSelectIndex];

                mo.httpClient.sendBuyShopItem(info.product_id, "1", mobileNum, function(isSuccess){
                    if (isSuccess){
                        this.Panel_second.setVisible(false);
                        this.mInputBox.setString("");
                    }else{
                        this.Panel_second.setVisible(false);
                    }
                    sender.setEnabled(true);
                }, this);

                sender.setEnabled(false);
            }
        }, this);

        //复制号码
        mo.seekWidgetByName(this.ccsNode, "Panel_tishi_copy").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.nativeHelper.copyString("qixinqipai");
                mo.showRoomMsgShow("复制成功");
            }
        }, this);


        this.Panel_second.setVisible(false);


        //修复剪切bug
        mo.gameHelper.setClippingLayout(this.PageView_list);
    },

    onEnter: function () {
        this._super();

        if (mo.shopListData == null){
            if (cc.sys.isNative){
                mo.httpClient.sendGetShopList(function(data){
                    mo.shopListData = data;
                    this.mData = data;
                    this.setGiftContent();
                }, this);
            }
        }else{
            this.mData = mo.shopListData;
            this.setGiftContent();
        }

        //更新自己的奖券
        mo.eventHelper.add("updateUserInfo", this, function(){
            var cost = 0;
            if (this.mData != null && this.mSelectIndex != null){
                var info = this.mData[this.mSelectIndex];
                cost = info.cost_item_num;
            }
            this.setLottery(mo.gameUserInfo.getCoupons(), cost);
        });

        //设置奖券信息
        this.setLottery(mo.gameUserInfo.getCoupons(), 0);
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    closeThis : function(){
        this.closeAction(this.Panel_center)
    },

    setGiftContent : function(){
        var len = this.mData.length;
        var childPanel = null;
        var xs = [130, 400, 800 - 130];
        var ys = [350 - 90, 90];

        this.mItems = [];

        for (var i = 0; i < len; i++){
            if (i % 6 == 0){
                childPanel = new ccui.Layout();
                this.PageView_list.addPage(childPanel);
            }

            var item = new KwxGiftExchangeItem();
            childPanel.addChild(item);

            var temp = i % 6;
            var col = temp % 3;
            var row = parseInt(temp / 3);
            item.setPosition(cc.p(xs[col], ys[row]));
            this.mItems.push(item);

            item.setTag(i);
            item.setItemInfo(this.mData[i]);
            item.addTouchEventListener(this.onTouchItem, this);
        }

        this.updateIndex();
    },

    onTouchItem : function(sender, type) {
        if (mo.gameHelper.getIsTouchEnd(type)) {
            var selectIndex = sender.getTag();
            for (var i = 0; i < this.mItems.length; i++){
                this.mItems[i].setSelect(i == selectIndex);
            }

            this.mSelectIndex = selectIndex;

            //更新
            this.setLottery(mo.gameUserInfo.getCoupons(), this.mData[selectIndex].cost_item_num);
        }
    },

    setLottery : function(my, cost){
        var myStr = "" + my;
        var costStr = "0";
        if (cost > 0){
            costStr = "" + cost;
        }

        mo.gameHelper.setTextString(this.Text_myLottery, myStr);
        mo.gameHelper.setTextString(this.Text_costLottery, costStr);

        this.Text_costLottery.setTextColor(cost > my ? cc.color(255, 0, 24) : cc.color(0, 156, 255));

        if (cost > 0 && my >= cost){
            this.Button_send.setEnabled(true);
        }else{
            this.Button_send.setEnabled(false);
        }
    },

    //更新分页小圆点
    updateIndex : function(){
        var count = this.PageView_list.getItems().length;
        var index = this.PageView_list.getCurrentPageIndex();
        var xAdd = 30;
        var startX = -(count - 1) * xAdd / 2;
        for (var i = 0; i < this.Image_fenyes.length; i++){
            if (i < count){
                this.Image_fenyes[i].setVisible(true);
                this.Image_fenyes[i].loadTexture(index == i ? "res/ccs/hall_second/gift/point0.png" : "res/ccs/hall_second/gift/point1.png");
                this.Image_fenyes[i].setPositionX(startX + xAdd * i);

            }else{
                this.Image_fenyes[i].setVisible(false);
            }
        }
    }
});

mo.shopListData = null;